﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Liju.Model;
using LiJu.Db;

namespace LiJu
{
    public partial class SongDingJudge : Form
    {
        /// <summary>
        /// 重新送钉判定实体
        /// </summary>
        public SongDingJudgeModel songDingJudgeModel = new SongDingJudgeModel();

        ///// <summary>
        ///// true 工作模式 false 测试模式
        ///// </summary>
        //public bool bIsWorkModel = true;

        /// <summary>
        /// 送钉判定状态 0待判定 1判定送 2判定不送 3已丢废螺丝 4送成功 5送失败 6取消
        /// </summary>
        public int nState = 0;

        public SongDingJudge()
        {
            InitializeComponent();
        }

        private void SongDingJudge_Load(object sender, EventArgs e)
        {
            tbGongHao.Text = tbPassWord.Text = tbReMark.Text = "";
            cbIssueType.SelectedIndex = 0;
        }

        private void BtTiJiao_Click(object sender, EventArgs e)
        {
            if (songDingJudgeModel != null)
            {
                if (tbGongHao.Text.Trim().Length < 1)
                {
                    tbGongHao.Focus();
                    MessageBox.Show("用户名不能为空。");
                    return;
                }
                if (tbPassWord.Text.Trim().Length < 1)
                {
                    tbPassWord.Focus();
                    MessageBox.Show("密码不能为空。");
                    return;
                }

                string sql = string.Format(@"SELECT * from usermanager WHERE (UserID='{0}' or UserName='{0}') and `Password`='{1}'", tbGongHao.Text.Trim(), EncryptionHelper.Encrypt(tbPassWord.Text.Trim()));
                var result = Db.DalMySQL.Query(sql);
                if (result.Tables[0].Rows.Count > 0)
                {
                    //有无送钉判定权限
                    var menuRole = PageDataDto.SystemMenuRoleInfo(tbGongHao.Text.Trim());
                    if (menuRole != null && !menuRole.Work_SongDing)
                    {
                        MessageBox.Show("抱歉，您没有重新送钉判定权限，请联系管理员设置权限！", "无操作权限", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    songDingJudgeModel.JudgeUserName = result.Tables[0].Rows[0][2].ToString();
                }
                else
                {
                    MessageBox.Show("请重新输入账号和密码！", "账号或密码输入错误 ！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    tbGongHao.Text = tbPassWord.Text = "";
                    tbGongHao.Focus();
                    return;
                }
                songDingJudgeModel.JudgeGongHao = tbGongHao.Text.Trim();
                if (rbtSongDing.Checked)
                {
                    songDingJudgeModel.State = 1;
                    songDingJudgeModel.OperateType =  1;
                }
                else
                {
                    songDingJudgeModel.State = 2;
                    songDingJudgeModel.OperateType = 0;
                }
                songDingJudgeModel.IssueType = cbIssueType.Text.Trim();
                songDingJudgeModel.ReMark = tbReMark.Text.Trim();
                songDingJudgeModel.CreateTime = DateTime.Now;
                //if (bIsWorkModel)
                {
                    Int64 nReturn = SongDingJudgeDbHelper.SongDingAgainAdd(songDingJudgeModel);
                    if (nReturn > 0)
                    {
                        songDingJudgeModel.LogID = nReturn;
                    }
                    else
                    {
                        MessageBox.Show("Error:行93 SongDingAgainAdd 添加送钉判定记录异常。");
                        return;
                    }
                }
                nState = songDingJudgeModel.State;
                this.Hide();
            }
        }

        private void BtCancel_Click(object sender, EventArgs e)
        {
            nState = 6;
            this.Hide();
        }

    }
}
